﻿-- Author      : Kamil
-- Create Date : 9/18/2012 10:36:34 PM
local mnk = CreateFrame("Frame");

-- zapisana konfiguracja
btnConfiguration = {};
panelConfiguration = {};
frmConfiguration = {};
bgRatting = {};

-- wersja oprogramowania
VERSION = 'MNK-HKM: ver. 4.2.2'

-- stale parametry
HELLOMESSAGE1 = '|cff22ff55 Witamy w Kill Us Or Die Trying!';
HELLOMESSAGE2 = '|cff22ff55 MnK-HKM załadowany!';
HIDEMESSAGE  = '|cff22ff55 MnK-HKM ukryty. Wpisz |cff224499/mnk |cff22ff55aby wywołać.';
ADDONCAPTION = 'MnK-addon';
WG_CLOCK_INTERVAL = 1;

-- komendy slash
SLASH_MNK1 = '/mnk'; 


-- zmienne globalne
InviteAutoAccept = false;
TimePlayed = 0;
LastClockUpdate = 0;
NextWGTime = nil;
BGAutoJoin = false;


-- wiadomosc powitalna
function ShowHelloMsg()
	print(HELLOMESSAGE1);
	print(HELLOMESSAGE2);
end;

-- slash commands
function SlashCmdList.MNK(msg, editbox)
	mainFrame:Show();
	if (msg == "conf") then configFrame:Show(); end;
	if (msg == "test") then print('wtf'); end;

end



-- ladowanie okna programu
function mainFrame_OnLoad()
	mainFrame:Show();
	titleCaption:SetText(ADDONCAPTION);
	RequestTimePlayed();
	ShowHelloMsg();
end

-- ukrywanie okna programu
function buttonHide_OnClick()
	mainFrame:Hide();
	print(HIDEMESSAGE);
end


-- obsluga zdarzen
function On_event(frameHandler, eventName)
	if (eventName == "CHAT_MSG_ADDON") then AddonMessageHandler(arg1, arg2, arg3, arg4, arg5, arg6, arg7); end
	
	if (eventName == "PARTY_INVITE_REQUEST") then PartyInviteEvent(arg1); end
	
	if (eventName == "CHAT_MSG_GUILD") then GuildMessageEvent(arg1, arg2, arg11, arg12);  end
	
	if (eventName == "TIME_PLAYED_MSG") then TimePlayed = floor(arg1/3600);  end
	
	if (eventName == "PLAYER_TARGET_CHANGED") then TargetChangedEvent(); end
	
	if (eventName == "PLAYER_DEAD") then PlayerKilledEvent(time()); end
	
	if (eventName=="COMBAT_LOG_EVENT_UNFILTERED") then CombatLogEventHandler(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11); end
	
	if (eventName=="CHAT_MSG_CHANNEL_JOIN") then PlayerJoinedChannelEvent(time(),arg2); end
	
	if (eventName=="MERCHANT_SHOW") then MerchantShowEvent(); end
	
	if (eventName=="PLAYER_LEVEL_UP") then PlayerLVLedEvent(arg1); end
	
	if (eventName=="ADDON_LOADED") then AddonLoadedEvent(arg1,time()); end
	
	if (eventName=="CHAT_MSG_BG_SYSTEM_NEUTRAL") then bgResultEvent(arg1,time()); end
end

function GetProfessions_click()
	SendAddonMessage(PX_SHOW_PROFESSIONS, FormatAddonMessage('ShowAll'), 'GUILD',nil); 
	LinkAllMyProfessions('GUILD');
end

function GetLatency_click()
	SendAddonMessage(PX_SHOW_LATENCY, FormatAddonMessage('ShowPing'), 'GUILD',nil);
end

function GuildInvite()
	SendAddonMessage(PX_PING_GUILD, FormatAddonMessage('Ping'), 'GUILD',nil);
end

function GetStatistics()
	SendAddonMessage(PX_SHOW_STATISTICS, FormatAddonMessage('All'), 'GUILD',nil);
end

function SendGuildWarning()
	SendAddonMessage(PX_GUILD_WARNING, FormatAddonMessage(gw_text:GetText()), 'GUILD',nil);
	gw_text:SetText('');
	echoFrame:Hide();
end

function GW_TextShowHide()
	if not (echoFrame:IsShown()) then echoFrame:Show(); else echoFrame:Hide(); end
end

function ConfigShowHide()
	if not (configFrame:IsShown()) then configFrame:Show(); else configFrame:Hide(); end
end

function FormatAddonMessage(msg)
	return msg .. '{' .. time() .. '} ';
end

function wgClockUpdate(timeStamp)
	--print(timeStamp - LastClockUpdate);
	if (timeStamp - LastClockUpdate >= WG_CLOCK_INTERVAL) then
		if not (GetWintergraspWaitTime() == nil) then NextWGTime = GetWintergraspWaitTime();end
		if (GetWintergraspWaitTime() == nil) then requestWGTime(); end
		
		wgTimer:SetText(formatWGTime(NextWGTime));
		LastClockUpdate = timeStamp;
	end;  
end

function setCheckBoxes()
	check_f_prof:SetChecked(btnConfiguration[0].f_prof);
	check_f_latency:SetChecked(btnConfiguration[0].f_latency);
	check_f_autoparty:SetChecked(btnConfiguration[0].f_autoparty);
	check_f_stats:SetChecked(btnConfiguration[0].f_stats);
	check_f_guildwarning:SetChecked(btnConfiguration[0].f_guildwarning);
	check_f_records:SetChecked(btnConfiguration[0].f_records);
	check_f_idontwantit:SetChecked(btnConfiguration[0].f_idontwantit);
	check_f_config:SetChecked(btnConfiguration[0].f_config);
	check_f_bgrat:SetChecked(btnConfiguration[0].f_bgrat);
end

function readCheckBoxes(checkBox)
	if (checkBox == "check_f_prof") then
		btnConfiguration[0].f_prof = check_f_prof:GetChecked();
	end
	
	if (checkBox == "check_f_latency") then
		btnConfiguration[0].f_latency = check_f_latency:GetChecked();
	end
	
	if (checkBox == "check_f_autoparty") then
		btnConfiguration[0].f_autoparty = check_f_autoparty:GetChecked();
	end
	
	if (checkBox == "check_f_stats") then
		btnConfiguration[0].f_stats = check_f_stats:GetChecked();
	end
	
	if (checkBox == "check_f_guildwarning") then
		btnConfiguration[0].f_guildwarning = check_f_guildwarning:GetChecked();
	end
	
	if (checkBox == "check_f_records") then
		btnConfiguration[0].f_records = check_f_records:GetChecked();
	end	
	
	if (checkBox == "check_f_idontwantit") then
		btnConfiguration[0].f_idontwantit = check_f_idontwantit:GetChecked();
	end	
	
	if (checkBox == "check_f_bgrat") then
		btnConfiguration[0].f_bgrat = check_f_bgrat:GetChecked();
	end	
	
	if (checkBox == "check_f_config") then
		btnConfiguration[0].f_config = check_f_config:GetChecked();
		if (btnConfiguration[0].f_config == nil) then print('|cff22ff55 Skrót do konfiguracji ukryty. Wpisz: |cff224499/mnk conf |cff22ff55aby wywołać.') ; end;
	end	
	
	redrawPanel();		
end

function test2()
	item_1:SetNormalTexture("Interface\\AddOns\\mnk\\Images\\latency.tga");
end

function redrawPanel()

	assignButton(1, "", nil);
	assignButton(2, "", nil);
	assignButton(3, "", nil);
	assignButton(4, "", nil);
	assignButton(5, "", nil);
	assignButton(6, "", nil);
	mainFrame:SetHeight((panelConfiguration[0].pnRows * 60)+10);
	mainFrame:SetWidth((panelConfiguration[0].pnCols * 60)+8);
	tb_txt:SetWidth((panelConfiguration[0].pnCols * 60)+8);
	mainFrame:SetScale(panelConfiguration[0].pnScale);
	
	local btn = 1;
	
	if (btnConfiguration[0].f_prof == 1) then		
		 assignButton(btn, textures['prof'],GetProfessions_click);
		 btn=btn+1;
	end;
	
	if (btnConfiguration[0].f_latency == 1) then		
		 assignButton(btn, textures['latency'],GetLatency_click);
		 btn=btn+1;
	end;
	
	if (btnConfiguration[0].f_autoparty == 1) then		
		 assignButton(btn, textures['autoparty'],GuildInvite);
		 btn=btn+1;
	end;
	
	if (btnConfiguration[0].f_stats == 1) then		
		 assignButton(btn, textures['stats'],GetStatistics);
		 btn=btn+1;
	end;
	
	if (btnConfiguration[0].f_guildwarning == 1) then		
		 assignButton(btn, textures['guildwarning'],GW_TextShowHide);
		 btn=btn+1;
	end;
	
	if (btnConfiguration[0].f_bgrat == 1) then		
		 assignButton(btn, textures['bgrat'],bgRatShowHide);
		 btn=btn+1;
	end;	
	
	if (btnConfiguration[0].f_config == 1) then		
		 assignButton(btn, textures['config'],ConfigShowHide);
		 btn=btn+1;
	end; 	
	
	if (btn > 7) then print('Panel jest biedny jak dzieci w Etiopii i nie ogarnie wiecej jak 6 ikon. Uszanuj to ;f'); end;
	--assignButton(btn, textures['config'],ConfigShowHide);
end

function assignButton(idBtn, txt, func)
	if (idBtn == 1) then
		item_1:SetNormalTexture(txt);
		item_1:SetScript("OnClick",func);
	end
	
	if (idBtn == 2) then
		item_2:SetNormalTexture(txt);
		item_2:SetScript("OnClick",func);
	end	
	
	if (idBtn == 3) then
		item_3:SetNormalTexture(txt);
		item_3:SetScript("OnClick",func);
	end
	
	if (idBtn == 4) then
		item_4:SetNormalTexture(txt);
		item_4:SetScript("OnClick",func);
	end	
	
	if (idBtn == 5) then
		item_5:SetNormalTexture(txt);
		item_5:SetScript("OnClick",func);
	end
	
	if (idBtn == 6) then
		item_6:SetNormalTexture(txt);
		item_6:SetScript("OnClick",func);
	end			
end

function setKillFrameLocation(offset)
	if not (frmConfiguration[0] == nil) then
		if (offset == 'x') then frmConfiguration[0].killInfo_x = killInfoLocation:GetValue(); end;
		if (offset == 'y') then frmConfiguration[0].killInfo_y = killInfoLocation2:GetValue(); end;
		setFrames();
		KillsMsgFrame:AddMessage("Kill Us Or Die Trying!", 0.8, 0.8 , 0, 52, 1);
	end;
end

function setFrames()
	KillsMsgFrame:SetPoint("TOP", GuildMsgFrame, "BOTTOM", frmConfiguration[0].killInfo_x, frmConfiguration[0].killInfo_y);
end

function setPanelScale()
	panelConfiguration[0].pnScale = (panelScale:GetValue()/100);
	mainFrame:SetScale(panelConfiguration[0].pnScale);
end

function bgRatShowHide()
	if not (specialIconFrame:IsShown()) then specialIconFrame:Show(); else specialIconFrame:Hide(); end
end

function setRattingLabels()
	local playedBG = GetStatistic(839);
	local wonBG = GetStatistic(840);
	if (playedBG == "--") then playedBG = 0; end;
	if (wonBG == "--") then wonBG = 0; end;	
	local losBG = playedBG - wonBG;

	bgLos_l:SetText(losBG);
	bgWins_l:SetText(wonBG);
	bgRatting_l:SetText(bgRatting[UnitName('player')].ratting);
	player_l:SetText(UnitName('player'));
end

function getLinkRattings()
	SendAddonMessage(PX_GET_RATTING, FormatAddonMessage('GetRat'), 'GUILD',nil);
	--value = GetStatistic(achievementID)
	--print(GetStatisticId('Battlegrounds', 'Battlegrounds won'));

end

function GetStatisticId(CategoryTitle, StatisticTitle)
	local str = ""
	for _, CategoryId in pairs(GetStatisticsCategoryList()) do
		local Title, ParentCategoryId, Something
		Title, ParentCategoryId, Something = GetCategoryInfo(CategoryId)
		
		if Title == CategoryTitle then
			local i
			local statisticCount = GetCategoryNumAchievements(CategoryId)
			for i = 1, statisticCount do
				local IDNumber, Name, Points, Completed, Month, Day, Year, Description, Flags, Image, RewardText
				IDNumber, Name, Points, Completed, Month, Day, Year, Description, Flags, Image, RewardText = GetAchievementInfo(CategoryId, i)
				if Name == StatisticTitle then
					return IDNumber
				end
			end
		end
	end
	return -1
end


mnk:SetScript("OnEvent", On_event);
mnk:RegisterEvent("CHAT_MSG_ADDON");
mnk:RegisterEvent("PARTY_INVITE_REQUEST");
mnk:RegisterEvent("CHAT_MSG_GUILD");
mnk:RegisterEvent("TIME_PLAYED_MSG");
mnk:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED");
mnk:RegisterEvent("PLAYER_TARGET_CHANGED");
mnk:RegisterEvent("PLAYER_DEAD");
mnk:RegisterEvent("CHAT_MSG_CHANNEL_JOIN");
mnk:RegisterEvent("PLAYER_LEVEL_UP");
mnk:RegisterEvent("MERCHANT_SHOW");
mnk:RegisterEvent("ADDON_LOADED");
mnk:RegisterEvent("UPDATE_BATTLEFIELD_STATUS");
mnk:RegisterEvent("CHAT_MSG_BG_SYSTEM_NEUTRAL");